import { Module, NestModule, MiddlewareConsumer, RequestMethod, INestApplication } from '@nestjs/common';
import { AppController } from './app.controller';
import { AppService } from './app.service';
import { CatsController } from './cats/cats.controller';
import { AccountController } from './account/account.controller';
import { CatsService } from './cats/cats.service';
import { CatsModule } from './cats/cats.module';
import { LoggerMiddleware, logger } from './middlewares/logger.middleware'
import { PostsModule } from './posts/posts.module';

// 模块是单例的
@Module({
    imports: [CatsModule, PostsModule],
    // 有了 cats模块就不需要把cat service和cat controller都引过来了
    // controllers: [AppController, CatsController, AccountController],
    // providers: [AppService, CatsService],
})
export class AppModule implements NestModule {
    configure(consumer: MiddlewareConsumer) {
    // consumer.apply(LoggerMiddleware)
    //   .forRoutes({
        //path: 'cats', // 精准匹配，http://localhost:3000/cats，在http://localhost:3000/cats/book 上不生效
        //path: 'cats/*', // 不匹配 /cats
        // path: 'cats*?', // 匹配 /cats 和后面加任意字符
        // method: RequestMethod.GET
        // })

        consumer.apply(LoggerMiddleware, logger)
            .exclude({
                path: 'cats',
                method: RequestMethod.GET
            })
            .forRoutes(CatsController)

    }
}
// 中间件全局注册在main.ts中
